package cn.liccshop.core.utils;

import cn.liccshop.core.entity.SysSysMenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiCC
 * @program LiCCShop
 * @description 系统菜单工具类
 * @since 2019-04-05-20:58
 */
public class SysSysMenuUtils {
    /**
     * @description 管理员导航栏数据整合成管理员前端需要的格式
     * @return
     */
    public static List<SysSysMenu> sysMenuListIntegration(List<SysSysMenu> menus){
        List<SysSysMenu> menuList = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++){
            SysSysMenu menu = menus.get(i);
            if (menu.getType() != 1) {
                // 由于数据是先按菜单等级升序再按排序升序，所以当前不是一级后面都不再是一级菜单，可以跳出循环了
                break;
            }
            SysSysMenu menuinfo = new SysSysMenu();
            List<SysSysMenu> children  = new ArrayList<>();
            menuinfo.setPath(menu.getPathFirst())
                    .setComponent(menu.getComponentFirst());
            for (int j = i + 1; j < menus.size(); j++){
                SysSysMenu menuChild = menus.get(j);
                if (menuChild.getType() == 1){
                    // 如果是一级跳过
                    continue;
                }
                if (menuChild.getParentId().equals(menu.getId())){
                    // 添加子导航栏
                    Map<String, Object> meta = new HashMap<>();
                    meta.put("title",menuChild.getTitle());
                    if (menuChild.getIcon() != null){
                        meta.put("icon",menuChild.getIcon());
                    }
                    if (menuChild.getNoCache() != null){
                        meta.put("noCache",menuChild.getNoCache());
                    }
                    children.add(new SysSysMenu().setPath(menuChild.getPath())
                                    .setComponent(menuChild.getComponent())
                                    .setName(menuChild.getName())
                                    .setMeta(meta));
                }
            }
            if (children.size() >0){
                // 存在子导航栏
                Map<String, Object> meta = new HashMap<>();
                meta.put("title",menu.getTitle());
                if (menu.getIcon() != null){
                    meta.put("icon",menu.getIcon());
                }
                menuinfo.setRedirect(menu.getRedirect())
                        .setName(menu.getName())
                        .setMeta(meta)
                        .setChildren(children);
            } else {
                // 不存在子导航栏
                Map<String, Object> meta = new HashMap<>();
                meta.put("title",menu.getTitle());
                if (menu.getIcon() != null){
                    meta.put("icon",menu.getIcon());
                }
                if (menu.getNoCache() != null){
                    meta.put("noCache",menu.getNoCache());
                }
                children = new ArrayList<>();
                children.add(new SysSysMenu().setPath(menu.getPath())
                        .setName(menu.getName())
                        .setComponent(menu.getComponent())
                        .setMeta(meta));
                menuinfo.setChildren(children);
            }
            menuList.add(menuinfo);
        }
        menuList.add(new SysSysMenu().setPath("*").setRedirect("/404").setHidden("true"));
        return menuList;
    }
    /**
     * @description 管理员导航栏数据整合成管理员前端需要的格式
     * @return
     */
    public static List<SysSysMenu> userInfoOfMenus(List<Map<String, Object>> menus){
        List<SysSysMenu> menuList = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++){
            Map<String, Object> menu = menus.get(i);
            if (Integer.parseInt(menu.get("type").toString()) != 1) {
                // 由于数据是先按菜单等级升序再按排序升序，所以当前不是一级后面都不再是一级菜单，可以跳出循环了
                break;
            }
            SysSysMenu menuinfo = new SysSysMenu();
            List<SysSysMenu> children  = new ArrayList<>();
            menuinfo.setPath(menu.get("pathFirst").toString())
                    .setComponent(menu.get("componentFirst").toString());
            for (int j = i + 1; j < menus.size(); j++){
                Map<String, Object> menuChild = menus.get(j);
                if (Integer.parseInt(menuChild.get("type").toString()) == 1){
                    // 如果是一级跳过
                    continue;
                }
                if (menuChild.get("parentId").equals(menu.get("id"))){
                    // 添加子导航栏
                    Map<String, Object> meta = new HashMap<>();
                    meta.put("title",menuChild.get("title"));
                    if (menuChild.get("icon") != null){
                        meta.put("icon",menuChild.get("icon"));
                    }
                    if (menuChild.get("noCache") != null){
                        meta.put("noCache",menuChild.get("noCache"));
                    }
                    children.add(new SysSysMenu().setPath(menuChild.get("path").toString())
                            .setComponent(menuChild.get("component").toString())
                            .setName(menuChild.get("menuName").toString())
                            .setMeta(meta));
                }
            }
            if (children.size() >0){
                // 存在子导航栏
                Map<String, Object> meta = new HashMap<>();
                meta.put("title",menu.get("title"));
                if (menu.get("icon") != null){
                    meta.put("icon",menu.get("icon"));
                }
                menuinfo.setRedirect(menu.get("redirect").toString())
                        .setName(menu.get("menuName").toString())
                        .setMeta(meta)
                        .setChildren(children);
            } else {
                // 不存在子导航栏
                Map<String, Object> meta = new HashMap<>();
                meta.put("title",menu.get("title"));
                if (menu.get("icon") != null){
                    meta.put("icon",menu.get("icon"));
                }
                if (menu.get("noCache") != null){
                    meta.put("noCache",menu.get("noCache"));
                }
                children = new ArrayList<>();
                children.add(new SysSysMenu().setPath(menu.get("path").toString())
                        .setName(menu.get("menuName").toString())
                        .setComponent(menu.get("component").toString())
                        .setMeta(meta));
                menuinfo.setChildren(children);
            }
            menuList.add(menuinfo);
        }
        menuList.add(new SysSysMenu().setPath("*").setRedirect("/404").setHidden("true"));
        return menuList;
    }
}
